package com.bsoft.gol.editor.service.impl;

import com.bsoft.gol.editor.dao.EditorOrganizationDAO;
import com.bsoft.gol.editor.service.EditorOrganizationApi;
import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.EditorOrganization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author sangshuang
 */
@Service("editorOrganizationApi")
@Transactional
public class EditorOrganizationApiImpl implements EditorOrganizationApi {

    @Autowired
    EditorOrganizationDAO editorOrganizationDAO;


    @Override
    public BaseResponse add(EditorOrganization req)  {
        BaseResponse<EditorOrganization> baseResponse = new BaseResponse();
        if(CommonUtils.isEmpty(req.getOrganizationId())||CommonUtils.isEmpty(req.getOrganizationName())){
            baseResponse.setCode(500);
            baseResponse.setMessage("机构名称与机构id均不能为空");
            return baseResponse;
        }
        EditorOrganization editorOrganization = editorOrganizationDAO.getEditorOrganizationById(req.getOrganizationId());
        if(editorOrganization!=null){
            baseResponse.setCode(500);
            baseResponse.setMessage("该机构id已被使用");
        }else {
            req.setId(UUID.randomUUID().toString());
            req.setCreateTime(new Date());
            editorOrganizationDAO.save(req);
            baseResponse.setMessage("注册成功");
        }

        return baseResponse;
    }


    @Override
    public BaseResponse list(EditorOrganization req)  {

        BaseResponse<List<EditorOrganization>> baseResponse = new BaseResponse();
        List<EditorOrganization> editorOrganizationList= editorOrganizationDAO.queryEditorOrganizatiorList();
        baseResponse.setData(editorOrganizationList);


        return baseResponse;
    }
}
